package com.example.demo0522;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity {
    private Button btnReg;
    private Button btnLogin;
    private Button btnUpdateUser;
    private Button btnAdd;
    private Button btnQuery;
    private Button btnUpdate;
    private Button btnDel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initBtns();
    }

    private void initBtns() {
        btnReg = findViewById(R.id.btn_reg);
        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                regUser();
            }
        });

        btnLogin = findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginUser();
            }
        });

        btnUpdateUser = findViewById(R.id.btn_updateUser);
        btnUpdateUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateUser();
            }
        });


    }

    private void updateUser() {
        MyUser myUser = new MyUser();
        if (myUser==null){
            toast("请先登录");
            return;
        }

        myUser.setAge(19);
        myUser.setGender("男");
        myUser.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e==null){
                    toast("更新用户信息成功");
                }else {
                    toast("更新用户信息成功"+e.getMessage());
                }
            }
        });
    }

    private void loginUser() {
        BmobUser bmobUser = new BmobUser();
        bmobUser.setUsername("王源");
        bmobUser.setPassword("123456");
        bmobUser.login(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if (e==null){
                    toast("登陆成功"+bmobUser.getUsername());
                }else {
                    toast("登陆失败"+e.getMessage());
                }

            }
        });
    }

    private void regUser() {
        BmobUser bmobUser = new BmobUser();
        bmobUser.setUsername("王源");
        bmobUser.setPassword("123456");
        bmobUser.signUp(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if (e==null){
                    toast("注册成功"+bmobUser.getObjectId());
                }else {
                    toast("注册失败"+e.getMessage());
                }
            }
        });
    }

    private void toast(String s) {
        Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
    }
}
